package com.yunchi.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import cn.hutool.core.date.DateUtil;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: Crazer
 * @Date: 2023/7/4 13:32
 * @version: 1.0.0
 * @Description: 发送短信工具类
 */
public class SMSUtils {

    private static final Logger logger = LoggerFactory.getLogger(SMSUtils.class);

    /**
     * @param requestUrl  请求地址
     * @param phone       短信接收人电话号码
     * @param sendContent 短信发送内容
     * @return
     * @throws Exception
     */
    public static boolean sendSMS(String requestUrl, String phone, String sendContent) {

        // // 测试环境
        // String url = "http://172.18.100.112/SMS_WebService/BaseService.asmx";
        // // 生产环境
        // // String url = "http://192.168.100.89/SMS_WebService/BaseService.asmx";

        String namespace = "http://tempuri.org/";
        String actionUri = "Sender";
        String op = "Sender";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        String date = df.format(new Date());
        // 判断
        SimpleDateFormat hhtime = new SimpleDateFormat("HHmm");
        String hhmm = hhtime.format(new Date());
        int time = Integer.parseInt(hhmm);
        String result = "";
        // 发送短信内容
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<TranMessage>" +
                "<Message>" +
                "<SenderModCode>SMS_Host</SenderModCode>" +
                "<SenderInfo></SenderInfo>" +
                "<Content>" + sendContent + "</Content>" +
                "<SendTime>" + date + "</SendTime>" +
                "<AcceptTime>" + date + "</AcceptTime>" +
                "</Message>" +
                "<MsgRecvLists>" +
                "<MsgRecvList>" +
                "<ReceiverModCode>SMS_Host</ReceiverModCode>" +
                "<ReceiverInfo>" + phone + "</ReceiverInfo>" +
                "<RecvTime>" + date + "</RecvTime>" +
                "<TryTimes>1</TryTimes>" +
                "</MsgRecvList>" +
                "</MsgRecvLists>" +
                "</TranMessage>";
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(requestUrl));
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(namespace + actionUri); // action uri
            call.setOperationName(new QName(namespace, op));// 设置要调用哪个方法
            // 设置参数名称，具体参照从浏览器中看到的
            call.addParameter(new QName(namespace, "xml"), XMLType.XSD_STRING, ParameterMode.IN);  // 设置请求参数及类型
            // call.setReturnType(new QName(namespace,"getinfo"),Model.class); //设置返回结果为是某个类
            call.setReturnType(XMLType.XSD_STRING);// 设置结果返回类型
            Object[] params = new Object[1];
            params[0] = xml;
            result = (String) call.invoke(params); // 方法执行后的返回值
        } catch (Exception e) {
            logger.error("发送短信出现异常：\n", e);
            return false;
        }

        // 当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        String opeationTime = DateUtil.now();
        if ("true".equals(result)) {
            logger.info("运行时间：{}，向：{} 手机号，短信发送成功", opeationTime, phone);
            return true;
        } else {
            logger.error("运行时间：{}，向：{} 手机号，短信发送失败", opeationTime, phone);
            return false;
        }
    }
}